package ezXpns.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import ezXpns.Profile;
import ezXpns.Report;
import ezXpns.GUI.controllers.ReportController;
import ezXpns.analysis.PieChart;
import ezXpns.utils.DateTimeUtils;

import net.miginfocom.swing.MigLayout;
import javax.swing.DefaultComboBoxModel;

import java.awt.Font;


/**
 * @author A0086931X
 */

//@author A0086931X
public class AnalysisGUIPanel extends JPanel {
	public AnalysisGUIPanel(Report report, Profile profile) {
		this.report = report;
		this.profile = profile;

		setLayout(new MigLayout("", "[][:123.00:132.00,fill][50.00,left][][][][][]", "[][30][30][30][30][77.00][270,top][19.00][]"));

		JLabel lblPowerSearch = new JLabel("Power Search");
		add(lblPowerSearch, "cell 6 0,alignx center");
		JLabel lblIncome = new JLabel("Total Income");
		lblIncome.setFont(new Font("SansSerif", Font.BOLD, 16));
		add(lblIncome, "cell 1 1,alignx center");
		JLabel lblSpending = new JLabel("Total Spending");
		lblSpending.setFont(new Font("SansSerif", Font.BOLD, 16));
		add(lblSpending, "cell 1 2,alignx center");
		JLabel lblSavings = new JLabel("Total Savings");
		lblSavings.setFont(new Font("SansSerif", Font.BOLD, 16));
		add(lblSavings, "cell 1 3,alignx center");
		
		lblTagCloud = new JLabel("Tag Cloud (based on expenditure)");
		add(lblTagCloud, "cell 6 3");
		JLabel lblBudget = new JLabel("Monthly Budget");
		lblBudget.setFont(new Font("SansSerif", Font.BOLD, 16));
		add(lblBudget, "cell 1 4,alignx center");

		lblDisplayIncome = new JLabel();
		lblDisplaySavings = new JLabel();
		lblDisplayBudget = new JLabel();
		lblDisplaySpending = new JLabel();

		add(lblDisplayIncome, "cell 2 1");
		add(lblDisplaySavings, "cell 2 3");
		add(lblDisplayBudget, "cell 2 4");
		add(lblDisplaySpending, "cell 2 2");

		// other stuff.
		comboBox = new JComboBox<String>();
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] { "This Month", "This Year",
				"From The Beginning Of Time"}));
		comboBox.setEditable(true);
		add(comboBox, "cell 6 1,growx");

		pieChart = new PieChart("Payment Type Analysis");
		add(pieChart, "cell 1 5 2 2");

		cloudPanel = new CloudPanel();
		add(cloudPanel, "cell 6 4 2 4,alignx left,aligny top");
		
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getNewAnalysis(comboBox.getSelectedIndex()); // comboBox.getSelectedItem().toString());
			}
		});

	}

	public void connect(ReportController controller) {
		this.controller = controller;
		getNewAnalysis(0);
	}

	public void getNewAnalysis(int select) {
		Date today = new Date();

		//System.out.println(today);
		switch (select) {
		case 0:
			// month
			controller.requestAnalysis(DateTimeUtils.getMonthRange(today));
			break;
		case 1:
			// year
			controller.requestAnalysis(DateTimeUtils.getYearRange(today));
			break;
		case 2:
			// end of time
			controller.requestAnalysis(DateTimeUtils.getBeginningRange(today));
			break;
		case 3:
			AdvancedSearchDialog advancedSearchDialog = new AdvancedSearchDialog(null);
			advancedSearchDialog.setVisible(true);
			break;
		default:
			// prompt
			break;
		}
	}

	public void refreshReport(Report newReport) {
		report = newReport;

		float savings = report.getSavings();
		float totalIncome = report.getTotalIncome();
		float totalExpense = report.getTotalExpense();

		if (totalIncome > 0) {
			lblDisplayIncome.setText("$" + String.format("%.2f", totalIncome));
			lblDisplaySavings.setText("$" + String.format("%.2f", savings));
		} else {
			lblDisplayIncome.setText("No Income");
			lblDisplaySavings.setText("-");
		}

		lblDisplaySpending.setText("$" + String.format("%.2f", totalExpense));
		lblDisplayBudget.setText("$" + String.format("%.2f", profile.getBudget()));

		// pieChart updating
		if (report != null) {
			pieChart.inputData(report.getExpenseHash());
			pieChart.refreshPie();
		}
		
		// cloud updating
		if(report != null){
			cloudPanel.setTagHashMap(report.getTagHash());
			cloudPanel.generateCloud();
		}
		
		validate();
	}

	public void refresh() {
		refreshReport(report);
		comboBox.setSelectedIndex(0);
	}


	private static final long serialVersionUID = 1L;
	private Report report;
	private Profile profile;
	private JLabel lblDisplayIncome;
	private JLabel lblDisplaySavings;
	private JLabel lblDisplayBudget;
	private JLabel lblDisplaySpending;
	private JComboBox<String> comboBox;
	private ReportController controller;
	private PieChart pieChart;
	private CloudPanel cloudPanel;
	private JLabel lblTagCloud;
}
